import React, {PureComponent} from 'react';
import {Form,Table,Tooltip, Popconfirm, Switch,Button, Icon} from 'antd';
import styles from './index.less';
import GoodsAttr from '../../../routes/goods/attribute/GoodsAttr';
import {connect} from 'dva';
import {powerControl} from '../../../utils/powerControl';
const AuthButton = powerControl(Button);

class GoodsTypeList extends PureComponent {
    state = {
        selectedRowKeys: [],
        totalCallNo: 0,
        expandedRowKeys: []
    };

    componentWillReceiveProps(nextProps) {
        if (nextProps.selectedRows.length === 0) {
            this.setState({
                selectedRowKeys: [],
                totalCallNo: 0,
            });
        }
    }

    handleTableChange = (pagination, filters, sorter) => {
        this.props.onChange(pagination, filters, sorter);
    }

    handleEditRow = (record, pagination) => {
        this.props.onEditRow(record, pagination);
    }
    handleDelRow = (catId, pagination) => {
        this.props.onDelRow(catId, pagination);
    }
    handleAttrList=(record)=>{
       this.props.handleAttrList(record);
    }
    expandedRowRender = (record, pagination) => {
        return (
            <GoodsAttr cat_id = {record.cat_id} cat_name={record.cat_name} pagination = {pagination} />
        )
    }
    onExpandedRowsChange =(expandedRows) =>{

        let expanded = [];
        expandedRows.map(function(k,index){
            if (index === expandedRows.length - 1){
                expanded.push(k);
            }

        });
        this.setState({
            expandedRowKeys: expanded
        })
    }
    render() {

        const {selectedRowKeys, totalCallNo} = this.state;
        const {data: {list, pagination}, loading, expandedRowRender} = this.props;
        const status = ['可用', '不可用'];
        const columns = [
            {
                title: '分类名称', dataIndex: 'cat_name',//分类名称
                render: (val, record) => {
                    return <Tooltip placement="right" title={record.cat_name}>
                        <span>{val}</span>
                    </Tooltip>
                }
            },
            {
                title: '类型状态',
                dataIndex: 'enabled',//类型状态：1可用，0不可用
                filters: [
                    {
                        text: status[1],
                        value: "0"
                    },
                    {
                        text: status[0],
                        value: "1",
                    }
                ],
                filterMultiple: false,
                render(val, record) {
                    return <Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="cross"/>}
                                   defaultChecked={val} checked={val}/>;
                },
            }, {
                title: '属性分组',
                dataIndex: 'attr_group',//类型状态：1可用，0不可用
            }, {
                title: '操作',
                render: (val, record) => (
                    <p>
                        <AuthButton ghost auth="goodsType.edit" className={styles.btn}  onClick={() => this.handleEditRow(record, pagination)}>编辑</AuthButton>
                        <span className={styles.splitLine}/>
                        <Popconfirm title="是否确定删除?" onConfirm={() => this.handleDelRow(record.cat_id, pagination)} >
                            <AuthButton ghost auth="goodsType.delete" className={styles.btn}>删除</AuthButton>
                        </Popconfirm>
                        <span className={styles.splitLine}/>
                        <AuthButton ghost auth="goodsType.attrlist" className={styles.btn}  onClick={()=>this.handleAttrList(record)}>属性列表</AuthButton>
                    </p>
                ),
            },
        ];

        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            },
            onSelect: (record, selected, selectedRows) => {
                console.log(record, selected, selectedRows);
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
                console.log(selected, selectedRows, changeRows);
            },
        };

        const paginationProps = {
            showSizeChanger: true,
            showQuickJumper: true,
            ...pagination,
        };

        return (
            <div className={styles.standardTable} >
                <Table
                    loading={loading}
                    rowKey={record => record.cat_id}
                    // rowSelection={rowSelection}
                    dataSource={list}
                    columns={columns}
                    pagination={paginationProps}
                    onChange={this.handleTableChange}
                    // expandedRowRender={this.expandedRowRender}
                    // onExpandedRowsChange={this.onExpandedRowsChange}
                    //expandedRowKeys={this.state.expandedRowKeys}
                />
            </div>
        );
    }
}

export default GoodsTypeList;
